<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="doAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doEditor(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doDelete(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <el-dialog :visible.sync="flag" :title="isEdit ? '编辑部门' : '添加部门'" :close-on-click-modal="false" :close-on-press-escape="false">
          <AddorEdit v-if="flag" :pid="curId" :is-editor="isEdit" :up-date-list="upDateList" @offWindows="offWindows" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/department'
import { recursive } from '@/utils/index.js'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      list: [],
      flag: false,
      curId: '',
      isEdit: '',
      upDateList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 渲染部门结构
    async loadDepartments() {
      const res = await getDepartments()
      this.list = res.data.depts
      this.list.shift()
      this.list = recursive(res.data.depts, '')
      this.upDateList = res.data.depts.map(item => {
        return {
          name: item.name, code: item.code, pid: item.pid, introduce: item.introduce, id: item.id
        }
      })
    },
    // 弹出添加窗口
    doAdd(id) {
      this.isEdit = false
      this.flag = true
      this.curId = id
    },
    // 删除部门
    async doDel(id) {
      this.$confirm('你确定要删除此部门吗?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartments(id)
        this.loadDepartments()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 调用删除
    doDelete(id) {
      this.doDel(id)
    },
    offWindows() {
      this.flag = false
      this.loadDepartments()
    },
    // 编辑
    doEditor(id) {
      this.flag = true
      this.curId = id
      this.isEdit = true
      // 解决方案二
      // this.$nextTick(() => {
      //   console.log(this.refs.refDepdialog.loadDepartments())
      //   this.refs.refDepdialog.loadDepartments()
      // })
    }
  }
}
</script>
